/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package vadung.contragame2d;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 *
 * @author dung.voanh
 */
public class MySprite {
    protected int m_iID; // Sprite ID
    protected String m_strName;
    protected int m_iImageID; // Image Index in s_aimgRes of ResourceManager
    protected Image m_imgSprite;
    protected int m_iTotalFrame;
    protected int m_iTotalState;
    protected int m_iX; // Position x topleft
    protected int m_iY; // Position y topleft
    protected int m_iWidth;    
    protected int m_iHeight;
    protected int m_iCurrentFrame; // Column index in m_imgSprite
    protected int m_iCurrentState; // Row index in m_imgSprite
    protected int m_iStransform;

    // Getter
    
    public int GetID() {
        return m_iID;
    }
    
    public int GetImageID() {
        return m_iImageID;
    }

    public String GetName() {
        return m_strName;
    }

    public int GetTotalState() {
        return m_iTotalState;
    }
    
    public int GetTotalFrame() {
        return m_iTotalFrame;
    }

    public int GetCurrentFrame() {
        return m_iCurrentFrame;
    }

    public int GetCurrentState() {
        return m_iCurrentState;
    }
    
    public int GetPositionX(){
        return m_iX;
    }
    
    public int GetPositionY(){
        return m_iY;
    }
    
    //Setter
    
    public void SetID(int  iID) {
        this.m_iID = iID;
    }
    
    public void SetName(String strName) {
        this.m_strName = strName;
    }

    public void SetImageID(int iImageID) {
        this.m_iImageID = iImageID;
    }

    public void SetTotalFrame(int iTotalFrame) {
        if(iTotalFrame > 0){
            this.m_iTotalFrame = iTotalFrame;
        }else{
            System.out.println("ERROR: Set total frame sprite: " + m_strName);
        }
    }


    public void SetTotalState(int iTotalState) {
        if(iTotalState > 0){
            this.m_iTotalState = iTotalState;
        }else{
            System.out.println("ERROR: Set total state sprite: " + m_strName);
        }
    }
    
    public void SetPositionX(int x){
        m_iX = x;
    }
    
    public void SetPositionY(int y){
        m_iY = y;
    }
    
    public void SetPosition(int x, int y){
        m_iX = x;
        m_iY = y;
    }

    public MySprite() {
        m_iTotalFrame = 1;
        m_iCurrentState = 1;
    }
    
    public MySprite(Image imgSprite, int totalFrame, int totalState) {
        Init(imgSprite, totalFrame, totalState, 0, 0);
    }

    public MySprite(MySprite sprite) {
        this.m_iID = sprite.m_iID;
        this.m_strName = sprite.m_strName;
        this.m_iImageID = sprite.m_iImageID;
        this.m_imgSprite = sprite.m_imgSprite;
        this.m_iTotalFrame = sprite.m_iTotalFrame;
        this.m_iTotalState = sprite.m_iTotalState;
        this.m_iX = sprite.m_iX;
        this.m_iY = sprite.m_iY;
        this.m_iWidth = sprite.m_iWidth;
        this.m_iHeight = sprite.m_iHeight;
        this.m_iCurrentFrame = sprite.m_iCurrentFrame;
        this.m_iCurrentState = sprite.m_iCurrentState;
        this.m_iStransform = sprite.m_iStransform;
    }
    
    

    public final void Init(){
        m_imgSprite = ResourceManager.GetInstance().GetImage(m_iImageID);
        Init(m_imgSprite, m_iTotalFrame, m_iTotalState, m_iX, m_iY);
    }
    
    protected final void Init(Image imgSprite, int iTotalFrame, int iTotalState, int x, int y) {
      
        this.m_imgSprite = imgSprite;
        this.m_iTotalFrame = iTotalFrame;
        this.m_iTotalState = iTotalState;
        this.m_iX = x;
        this.m_iY = y;
        this.m_iWidth = imgSprite.getWidth() / iTotalFrame;
        this.m_iHeight = imgSprite.getHeight() / iTotalState;;
        this.m_iCurrentFrame = 0;
        this.m_iCurrentState = 0;
        this.m_iStransform = DEFINE.TRANS_NONE;
    }
    
    public void NextFrame(){
        if(m_iCurrentFrame < m_iTotalFrame - 1){
            m_iCurrentFrame++;
        }else{
            m_iCurrentFrame = 0;
        }
    }
    
    public void PreviousFrame(){
        if(m_iCurrentFrame > 0){
            m_iCurrentFrame--;
        }else{
            m_iCurrentFrame = m_iTotalFrame - 1;
        }
    }
    
    public void SetCurrentFrame(int frame){
        if(frame >= 0 && frame < m_iTotalFrame){
            m_iCurrentFrame = frame;
        }
    }
    
    public void NextSate(){
        if(m_iCurrentState < m_iTotalState - 1){
            m_iCurrentState++;
        }else{
            m_iCurrentState = 0;
        }
    }
    
    public void PreviousSate(){
      if(m_iCurrentState > 0){
            m_iCurrentState--;
        }else{
            m_iCurrentState = m_iTotalState - 1;
        }
    }
    
    public void SetCurrentSate(int state){
        if(state >= 0 && state < m_iTotalState){
            m_iCurrentFrame = state;
        }      
    }   
    
    public void Render(Graphics g){        
        int xSrc = m_iCurrentFrame * m_iWidth;
        int ySrc = m_iCurrentState * m_iHeight;
        g.drawRegion(m_imgSprite, xSrc, ySrc, m_iWidth, m_iHeight, m_iStransform, m_iX, m_iY, Graphics.TOP|Graphics.LEFT);
    }    
//    
//    public boolean CheckCollision(MySprite other){
//        int wLeft = (this.m_iX > other.m_iX) ? this.m_iX : other.m_iX ;
//        int wTop = (this.m_iY > other.m_iY) ? this.m_iY : other.m_iY;
//        int wRight = (this.m_iX + this.m_iWidth);
//        if(wRight > other.m_iX + other.m_iWidth){
//            wRight = other.m_iX + other.m_iWidth;
//        }
//        
//        int wBottom = this.m_iY + this.m_iHeight;
//        if(wBottom > other.m_iY + other.m_iHeight){
//            wBottom = other.m_iY + other.m_iHeight;
//        }
//        
//        if(wLeft <= wRight && wTop <= wBottom){
//            return true;
//        }
//        return false;
//    }
}
